package com.platform.usercenter.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import com.platform.common.core.constant.UserConstants;

import com.platform.common.core.domain.entity.SysUser;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.core.utils.poi.ExcelUtil;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.core.web.page.TableDataInfo;
import com.platform.common.log.enums.BusinessType;
import com.platform.constants.SysUserConstants;
import com.platform.usercenter.domain.CalCalendar;
import com.platform.usercenter.domain.CalTeamMember;
import com.platform.usercenter.service.ICalTeamMemberService;
import com.platform.usercenter.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 班组成员Controller
 *
 * @author yinjinlu
 * @date 2022-06-05
 */
@RestController
@RequestMapping("/mes/cal/teammember")
public class CalTeamMemberController extends BaseController {
	@Resource
	private ICalTeamMemberService calTeamMemberService;

	@Resource
	private ISysUserService userService;


	/**
	 * 查询班组成员列表
	 */

	@GetMapping("/list")
	public TableDataInfo list( CalTeamMember calTeamMember ) {
		startPage( );
		List< CalTeamMember > list=calTeamMemberService.selectCalTeamMemberList( calTeamMember );
		for(CalTeamMember teamMember: list) {
			SysUser user=userService.selectUserById( teamMember.getUserId( ) );
			if(StringUtils.isNotEmpty( user.getAvatar( ) )) {
				teamMember.setAvatar( user.getAvatar( ) );
			}
		}
		return getDataTable( list );
	}

	/**
	 * 获取当天值班班组人员信息
	 *
	 * @param calCalendar
	 * @return
	 */
	@GetMapping("/getTheDayPerson")
	public AjaxResult getTheDayPerson( CalCalendar calCalendar ) {
		if(StringUtils.isEmpty( calCalendar.getCalendarType( ) )) {
			return AjaxResult.error( "参数缺失！" );
		}
		Date day=calCalendar.getDate( );
		List< CalCalendar > days=null;
		if(StringUtils.isNull( day )) {
			day=new Date( );
		}

		ArrayList< CalTeamMember > calTeamMembers=calTeamMemberService.getTheDayPersonByType( day,calCalendar.getCalendarType( ) );
		return AjaxResult.success( calTeamMembers );

	}

	/**
	 * 导出班组成员列表
	 */

	@PostMapping("/export")
	public void export( HttpServletResponse response,CalTeamMember calTeamMember ) {
		List< CalTeamMember > list=calTeamMemberService.selectCalTeamMemberList( calTeamMember );
		ExcelUtil< CalTeamMember > util=new ExcelUtil< CalTeamMember >( CalTeamMember.class );
		util.exportExcel( response,list,"班组成员数据" );
	}

	/**
	 * 获取班组成员详细信息
	 */

	@GetMapping(value="/{memberId}")
	public AjaxResult getInfo( @PathVariable("memberId") Long memberId ) {
		return AjaxResult.success( calTeamMemberService.selectCalTeamMemberByMemberId( memberId ) );
	}

	/**
	 * 新增班组成员
	 */

	@PostMapping
	public AjaxResult add( @RequestBody CalTeamMember calTeamMember ) {
		if(SysUserConstants.NOT_UNIQUE.equals( calTeamMemberService.checkUserUnique( calTeamMember ) )) {
			return AjaxResult.error( "用户"+calTeamMember.getNickName( )+"已分配过班组！" );
		}

		return toAjax( calTeamMemberService.insertCalTeamMember( calTeamMember ) );
	}

	/**
	 * 删除班组成员
	 */
	@DeleteMapping("/{memberIds}")
	public AjaxResult remove( @PathVariable Long[] memberIds ) {
		return toAjax( calTeamMemberService.deleteCalTeamMemberByMemberIds( memberIds ) );
	}
}
